﻿using AutoMapper.Internal.Mappers;
using EMS.system.Common;
using EMS.system.Entityies.MasterData.SupplierManagementModel;
using EMS.system.Entityies.WarehouseManagement.ProcurementWarehousingModel;
using EMS.system.IEntityAppService.MasterData.ProcurementWarehousingServices;
using EMS.system.IEntityAppService.MasterData.ProcurementWarehousingServices.ProcurementWarehousingServicesDto;
using EMS.system.IEntityAppService.MasterData.SupplierManagementIService.SupplierManagementDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace EMS.system.EntityAppService.MasterData.ProcurementWarehousingServices
{
    public class ProcurementWarehousAppService : ApplicationService, IProcurementWarehousAppService
    {
        private readonly IRepository<ProcurementWarehousing> _repository;

        public ProcurementWarehousAppService(IRepository<ProcurementWarehousing> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> InsertProcurementWarehousingServicesDto(ProcurementWarehousServicesDto dto)
        {
            var procurementWarehousServices = dto.MapTo<ProcurementWarehousing>();
            ProcurementWarehousing procurementWarehousing = await _repository.InsertAsync(procurementWarehousServices);
            return procurementWarehousing == null ? 0 : procurementWarehousing.Id;
        }

        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="SupplierManagementCode"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public (List<ProcurementWarehousServicesDto> units, int total) GetProcurementWarehousing(string? LbounListCode, int pageIndex, int pageSize)
        {
            var entity = _repository.GetListAsync().Result;
            var list = entity.Select(x => new ProcurementWarehousServicesDto
            {
                Id = x.Id,
                LbounListCode = x.LbounListCode,
                LbounListName = x.LbounListName,
                SupplierManagement = x.SupplierManagement,
                LbounTime = x.LbounTime,
                PurchaseOrderNumber = x.PurchaseOrderNumber,
                Dstatus = x.Dstatus
            }).ToList();
            if (!string.IsNullOrEmpty(LbounListCode))
            {
                list = list.Where(x => x.LbounListCode.Contains(LbounListCode)).ToList();
            }
            int total = list.Count;
            var pageCount = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return (pageCount, total);
        }

        /// <summary>
        /// 修改反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProcurementWarehousServicesDto> GetProcurementWarehousing(int id)
        {
            var list = _repository.GetListAsync().Result.FirstOrDefault(x => x.Id == id);
            var result = ObjectMapper.Map<ProcurementWarehousing, ProcurementWarehousServicesDto>(list);
            return result;
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProcurementWarehousServicesDto> GetUpdate(int id)
        {
            var entity = _repository.GetListAsync().Result.FirstOrDefault(x => x.Id == id);
            return new ProcurementWarehousServicesDto()
            {

                LbounListCode = entity.LbounListCode,
                LbounListName = entity.LbounListName,
                SupplierManagement = entity.SupplierManagement,
                LbounTime = entity.LbounTime,
                PurchaseOrderNumber = entity.PurchaseOrderNumber,
                Dstatus = entity.Dstatus
            };
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> UpdateProcurementWarehousing(ProcurementWarehousServicesDto dto)
        {
            var entity = _repository.GetListAsync().Result.FirstOrDefault(x => x.Id == dto.Id);
            entity.LbounListCode = dto.LbounListCode;
            entity.LbounListName = dto.LbounListName;
            entity.SupplierManagement = dto.SupplierManagement;
            entity.LbounTime = dto.LbounTime;
            entity.PurchaseOrderNumber = dto.PurchaseOrderNumber;
            entity.Dstatus = dto.Dstatus;
            var supplier = await _repository.UpdateAsync(entity);
            return supplier == null ? 0 : supplier.Id;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteProcurementWarehousing(int id)
        {
            await _repository.DeleteAsync(x => x.Id == id);
            return 1;
        }
    };
}

